﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace Out2Share
{
    public partial class FormConfiguration : Form
    {
        public ConfData conf;

        public FormConfiguration()
        {
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            System.Configuration.Configuration config =
             ConfigurationManager.OpenExeConfiguration(
                   ConfigurationUserLevel.None);
            string auth = "no";
            if (radioButton2.Checked) {
                auth = "win";
            }
            if (radioButton3.Checked)
            {
                auth = "basic";
            }
            if (radioButton4.Checked)
            {
                auth = "msonline";
            }
            conf.ht["CONFIGURATION_ENDPOINT_NAME"] = this.textBoxEndPoint.Text;
            conf.ht["CONFIGURATION_LIBRARY"] = this.textBoxLibrary.Text;
            conf.ht["CONFIGURATION_USER_NAME"] = this.textBoxUser.Text;
            conf.ht["CONFIGURATION_USER_PWD"] = this.textBoxPassword.Text;
            conf.ht["CONFIGURATION_AUTH"] = auth;
            conf.ht["CONFIGURATION_PROXY_SET"] = this.checkBoxProxy.Checked.ToString();
            conf.ht["CONFIGURATION_PROXY_ADDRESS"] = this.textBoxProxyAddress.Text;
            conf.ht["CONFIGURATION_PROXY_USER"] = this.textBoxProxyUser.Text;
            conf.ht["CONFIGURATION_PROXY_PWD"] = this.textBoxProxyPassword.Text;
            conf.ht["CONFIGURATION_FLAG"] = this.textBoxFlag.Text;
            conf.ht["CONFIGURATION_CATEGORY"] = this.textBoxCategory.Text;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormConfiguration_Load(object sender, EventArgs e)
        {
            if (conf.ht["CONFIGURATION_AUTH"] == "win")
            {
                radioButton1.Checked = false;
                radioButton2.Checked = true;
                radioButton3.Checked = false;
                radioButton4.Checked = false;
            }
            if (conf.ht["CONFIGURATION_AUTH"] == "basic")
            {
                radioButton1.Checked = false;
                radioButton2.Checked = false;
                radioButton3.Checked = true;
                radioButton4.Checked = false;
            }
            if (conf.ht["CONFIGURATION_AUTH"] == "msonline")
            {
                radioButton1.Checked = false;
                radioButton2.Checked = false;
                radioButton3.Checked = false;
                radioButton4.Checked = true;
            }

            this.textBoxEndPoint.Text = conf.ht["CONFIGURATION_ENDPOINT_NAME"];
            if (conf.ht.ContainsKey("CONFIGURATION_LIBRARY"))
            {
                this.textBoxLibrary.Text = conf.ht["CONFIGURATION_LIBRARY"];
            }
            this.textBoxUser.Text = conf.ht["CONFIGURATION_USER_NAME"];
            this.textBoxPassword.Text = conf.ht["CONFIGURATION_USER_PWD"];
            if (conf.ht["CONFIGURATION_PROXY_SET"] != "")
            {
                this.checkBoxProxy.Checked = bool.Parse(conf.ht["CONFIGURATION_PROXY_SET"]);
            }
            this.textBoxProxyAddress.Text = conf.ht["CONFIGURATION_PROXY_ADDRESS"];
            this.textBoxProxyUser.Text = conf.ht["CONFIGURATION_PROXY_USER"];
            this.textBoxProxyPassword.Text = conf.ht["CONFIGURATION_PROXY_PWD"];

            if (conf.ht.ContainsKey("CONFIGURATION_FLAG"))
            {
                this.textBoxFlag.Text = conf.ht["CONFIGURATION_FLAG"];
            }
            else {
                this.textBoxFlag.Text = "Follow up";
            }

            if (conf.ht.ContainsKey("CONFIGURATION_CATEGORY"))
            {
                this.textBoxCategory.Text = conf.ht["CONFIGURATION_CATEGORY"];
            }
            else
            {
                this.textBoxCategory.Text = "Archive";
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            textBoxUser.Enabled = false;
            textBoxPassword.Enabled = false;
        }
        
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            textBoxUser.Enabled = false;
            textBoxPassword.Enabled = false;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            textBoxUser.Enabled = true;
            textBoxPassword.Enabled = true;
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            textBoxUser.Enabled = true;
            textBoxPassword.Enabled = true;
        }

        private void checkBoxProxy_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxProxy.Checked)
            {
                textBoxProxyAddress.Enabled = true;
                textBoxProxyUser.Enabled = true;
                textBoxProxyPassword.Enabled = true;
            }
            else {
                textBoxProxyAddress.Enabled = false;
                textBoxProxyUser.Enabled = false;
                textBoxProxyPassword.Enabled = false;
            }
        }



        
    }
}
